package com.shopoo.oauth.app;

import java.util.List;

import javax.annotation.Resource;

import com.shopoo.dto.MultiResponse;
import com.shopoo.dto.Response;
import com.shopoo.oauth.api.MenuService;
import com.shopoo.oauth.app.assembler.AppAssembler;
import com.shopoo.oauth.app.converter.AppConverter;
import com.shopoo.oauth.domain.entity.Menu;
import com.shopoo.oauth.domain.repository.MenuRepository;
import com.shopoo.oauth.domain.service.MenuDomainService;
import com.shopoo.oauth.dto.clientobject.MenuCO;
import com.shopoo.oauth.dto.clientobject.MenuTreeDTO;
import com.shopoo.oauth.dto.cqe.MenuAddCmd;
import com.shopoo.oauth.dto.cqe.MenuQuery;
import com.shopoo.oauth.dto.cqe.MenuUpdateCmd;

import org.springframework.stereotype.Service;

/**
 * @Description 菜单服务实现
 * @Date 2020/10/29 4:49 下午
 * @Author <a href="mailto:android_li@sina.cn">Joe</a>
 **/
@Service
public class MenuServiceImpl implements MenuService {

	@Resource
	private MenuRepository menuRepository;

	@Resource
	private MenuDomainService menuDomainService;

	@Override
	public Response save(MenuAddCmd menuAddCmd) {
		Menu menu = AppConverter.INSTANCE.toMenu(menuAddCmd);
		menuRepository.save(menu);
		return Response.buildSuccess();
	}

	@Override
	public Response deleteByMenuId(Long menuId) {
		menuRepository.deleteByMenuId(menuId);
		return Response.buildSuccess();
	}

	@Override
	public MultiResponse<MenuTreeDTO> findByConditions(MenuQuery menuQuery) {
		Menu menu = AppConverter.INSTANCE.toMenu(menuQuery);
		List<Menu> oauthMenus = menuRepository.findByConditions(menu);
		List<MenuTreeDTO> list = AppAssembler.transferMenuTreeDTO(oauthMenus, 0L);
		return MultiResponse.of(list);
	}

	@Override
	public MultiResponse<MenuCO> findMenusByUserId(String userId) {
		List<Menu> menus = menuRepository.findMenuByUserId(userId);
		List<MenuCO> list = AppAssembler.transferMenuCOList(menus);
		return MultiResponse.of(list);
	}

	@Override
	public Response update(MenuUpdateCmd menuUpdateCmd) {
		Menu menu = AppConverter.INSTANCE.toMenu(menuUpdateCmd);
		menuDomainService.updateById(menu);
		return Response.buildSuccess();
	}
}
